<?php
class UsersController extends Controller {
	public function index() {
		$this->getSmarty ();
		$this->smarty->display ( "admin/users/users.tpl" );
	}
	// public function getpage() {
	// $this->getSmarty ();
	// require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
	// // START 数据库查询及分页数据
	// $userService = new UsersService( $this->getDB () );
	// $page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
	// $rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
	// $offset = ($page - 1) * $rows;
	// $result = array ();
	// $row_count = $userService->countNum ();
	// $result ["total"] = $row_count;
	// $user_infos = $userService->userPage ( $offset, $rows );
	// $result ["rows"] = $user_infos;
	// echo json_encode ( $result );
	// }
	function delete() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
		// START 数据库查询及分页数据
		$userService = new UsersService ( $this->getDB () );
		$id = trim ( $_POST ['id'] );
		$userService->delete ( $id );
		echo - 1;
	}
	function userOriginModify() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$dataService = new CommonDataService ( $this->getDB () );
		$userid = trim ( $_POST ['userid'] );
		$origin = trim ( $_POST ['integral'] );
		$type = trim ( $_POST ['type'] );
		$content = trim ( $_POST ['content'] );
		$desc = trim ( $_POST ['desc'] );
		if ($origin <= 0) {
			$origin = 0;
		}
		if ($type == 0) {
			$sql = "UPDATE USERS SET points=points + " . $origin . " WHERE userid ='" . $userid . "'";
		} else {
			$sql = "UPDATE USERS SET points=points - " . $origin . " WHERE userid ='" . $userid . "'";
		}
		$time  =date("Y-m-d_H-i-s");
		$id = uniqid();
		$input_data = array (
				'id' => $id,
				'userid' => $userid,
				'username' => $_POST ['username'],
				'points' => $origin,
				'record_type' => $type,
				'create_time' => $time,
				'points_type' => $content,
				'points_desc' => $desc 
		);
		$dataService->add ("user_points_record", $input_data);
		$dataService->exSql ( $sql );
	}
	function getUserOriginListpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'CommonDataService.class.php';
		// START 数据库查询及分页数据
		$dataService = new CommonDataService ( $this->getDB () );
		
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$offset = ($page - 1) * $rows;
		
		$userid = trim ( $_POST ['userid'] );
		$condition = " userid = '" . $userid . "'";
		$result = array ();
		
		$row_count = $dataService->countNum ( "user_points_record", $condition );
		$result ["total"] = $row_count;
		$resource_infos = $dataService->resourcePage ( "user_points_record", $offset, $rows, $condition );
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function getpage() {
		$this->getSmarty ();
		require_once SERVICE . DS . 'admin' . DS . 'UsersService.class.php';
		// START 数据库查询及分页数据
		$userService = new UsersService ( $this->getDB () );
		$page = isset ( $_POST ['page'] ) ? intval ( $_POST ['page'] ) : 1;
		$rows = isset ( $_POST ['rows'] ) ? intval ( $_POST ['rows'] ) : 10;
		$userid = trim ( $_POST ['userid'] );
		$username = trim ( $_POST ['username'] );
		$email = trim ( $_POST ['email'] );
		$condition = $this->getCondition ( $userid, $username, $email );
		$offset = ($page - 1) * $rows;
		$result = array ();
		
		$row_count = $userService->countNum ( $condition );
		$result ["total"] = $row_count;
		$resource_infos = $userService->resourcePage ( $offset, $rows, $condition );
		$result ["rows"] = $resource_infos;
		echo json_encode ( $result );
	}
	function getCondition($userid, $username, $email) {
		$condition = array ();
		if (! empty ( $userid )) {
			$condition ['userid'] = "userid like '%" . $userid . "%'";
		}
		if (! empty ( $username )) {
			$condition ['username'] = "username like '%" . $username . "%'";
		}
		if (! empty ( $email )) {
			$condition ['email'] = "email like '%" . $email . "%'";
		}
		
		$i = 0;
		foreach ( $condition as $key => $value ) {
			
			if ($i == count ( $condition ) - 1) {
				$conditions .= $value;
			} else {
				$conditions .= $value . " and ";
			}
			$i ++;
		}
		return $conditions;
	}
}